package cn.itcast.wushangxiantu.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.PaiHangBangAdapter;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import cn.itcast.wushangxiantu.UserData.UserPaiHang;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class PaiHang extends AppCompatActivity implements View.OnClickListener {

    private TextView tvDj;
    private TextView tvMoney;
    private ListView lv;
    private ImageView iv;
    private PaiHangBangAdapter adapter;
    private View viewDj;
    private View viewMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pai_hang);
        initView();
        getDjPaiHang();
    }

    private void initView() {
        tvDj = (TextView) findViewById(R.id.tvDj);
        tvMoney = (TextView) findViewById(R.id.tvMoney);
        lv = (ListView) findViewById(R.id.lv);
        iv = (ImageView) findViewById(R.id.iv);

        viewDj = (View) findViewById(R.id.viewDj);
        viewMoney = (View) findViewById(R.id.viewMoney);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvDj:
                changeMenu(0);
                break;
            case R.id.tvMoney:
                changeMenu(1);
                break;
        }
    }

    public void changeMenu(int num) {
        viewDj.setVisibility(View.INVISIBLE);
        viewMoney.setVisibility(View.INVISIBLE);
        switch (num) {
            case 0:
               viewDj.setVisibility(View.VISIBLE);
                lv.setAdapter(null);
                getDjPaiHang();
                break;
            case 1:
                viewMoney.setVisibility(View.VISIBLE);
                lv.setAdapter(null);
                getMoneyPaiHang();
                break;

        }
    }

    private void getMoneyPaiHang() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "need=moneypaihang", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("beibao1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("dj2", src);
                UserPaiHang user = null;
                try {
                    user = new Gson().fromJson(src, UserPaiHang.class);
                } catch (Exception e) {
                    user = null;
                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }

    private void getDjPaiHang() {
        Log.v("12313123123213", OkhttpUtil.URL_LOG + "need=djpaihangbang");
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "need=djpaihangbang", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("dj1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("dj2", src);
                UserPaiHang user = null;
                try {
                    user = new Gson().fromJson(src, UserPaiHang.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (adapter == null) {
                adapter = new PaiHangBangAdapter(PaiHang.this);
                tvDj.setOnClickListener(PaiHang.this);
                tvMoney.setOnClickListener(PaiHang.this);
            }
            switch (message.what) {
                case 1:
                    UserPaiHang userPaiHang = (UserPaiHang) message.obj;
                    if (null != userPaiHang) {
                        adapter.setPaiHang(userPaiHang, 0);
                        lv.setAdapter(adapter);
                    } else {
                        ToastUtil.toast(PaiHang.this, "服务器炸了");
                    }

                    break;
                case 2:
                    userPaiHang = (UserPaiHang) message.obj;
                    if (null != userPaiHang) {
                        adapter.setPaiHang((UserPaiHang) message.obj, 1);
                        lv.setAdapter(adapter);
                    } else {
                        ToastUtil.toast(PaiHang.this, "服务器炸了");
                    }
                    break;
            }
            return false;
        }
    });
}